from utils.video_img_change import get_frame_from_video, get_video_from_frame
from utils.background_cut.opencv_func import cut_bg
from utils.style_change.word_clould_func import wordcloud_trans
from utils.commonUtils import clear_dir
from utils.videoUtils import getMusic, addMusic, get_fps_from_video
from utils.imgUtils import resize_img


if __name__ == '__main__':

    # ------------------------------------------------------------------------------------------------------------------
    interval = 3   # 抽帧间隔，最小间隔为1，每帧都抽，该参数越大视频帧数越大，生成速度读越快(帧率越高码率越大，压制以后画质会变差)
    input_video_name = '小丑.mp4'
    output_video_name = input_video_name
    output_size = (1280, 720)
    audio_volumex = 10
    word_path = './text/test.txt'
    # ------------------------------------------------------------------------------------------------------------------

    # 1
    input_video_path = './production_process/1_input_video/'
    split_imgs_path = './production_process/2_split_imgs/'
    cut_bg_imgs_path = './production_process/3_style_change_imgs/'
    word_cloud_trans_imgs_path = './production_process/4_word_cloud_trans_imgs/'
    output_video_path = './production_process/4_output_video/'
    add_music_video_path = './production_process/5_add_music_video/'

    # 1-2 ---------------------------------------------------- 视频抽帧,帧图片大小调整到1280，720
    clear_dir(split_imgs_path)
    get_frame_from_video(video_name=input_video_path+input_video_name, interval=interval, save_path=split_imgs_path)
    resize_img(path=split_imgs_path, size=output_size)
    # --------------------------------------------------------

    # 2-3 ---------------------------------------------------- 剔除背景
    clear_dir(cut_bg_imgs_path)
    cut_bg(input_path=split_imgs_path, output_path=cut_bg_imgs_path, bg_color=(255, 255, 255), detect="person")
    # --------------------------------------- -----------------

    # 3-4 ---------------------------------------------------- 转换词云
    clear_dir(word_cloud_trans_imgs_path)
    wordcloud_trans(input_path=cut_bg_imgs_path, output_path=word_cloud_trans_imgs_path, word_path=word_path, font='方正粗黑宋简体')
    # --------------------------------------------------------

    # 4-5 ---------------------------------------------------- 生成视频
    # clear_dir(output_video_path)
    fps = get_fps_from_video(input_video_path + input_video_name) / interval  # 生成视频的帧速率
    # size = get_size_from_video(input_video_path + input_video_name)
    get_video_from_frame(img_path=word_cloud_trans_imgs_path, save_path=output_video_path+input_video_name, fps=fps, size=output_size)
    # --------------------------------------------------------

    # 5-6 ---------------------------------------------------- 源音频合成到生成视频
    # clear_dir(add_music_video_path)
    # 抽出原音频
    audio = getMusic(input_video_path+input_video_name)
    # 音视频合并
    addMusic(input_video_path=output_video_path+input_video_name, audio=audio, output_video_path=add_music_video_path+input_video_name, volumex=audio_volumex)
    # --------------------------------------------------------
